package com.bdqn.zhang.dao.impl;

import com.bdqn.zhang.dao.VivolyDao;
import com.bdqn.zhang.dao.impl.dbutil.DButil;
import com.bdqn.zhang.pojo.Memberinfo;
import com.bdqn.zhang.pojo.Vivogly;
import com.bdqn.zhang.pojo.Vivoly;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VivolyDaoimpl extends DButil implements VivolyDao {
    Connection conn = null;
    PreparedStatement ment = null;
    ResultSet resultSet = null;
    @Override
    public int add(Vivoly Vivoly) {
        String sql = "INSERT INTO Vivoly(name,phone,neirong,zhuangtai) VALUES(?,?,?,?)";
        Object[] obj = {Vivoly.getName(),Vivoly.getPhone(),Vivoly.getNeirong(),Vivoly.getZhuangtai()};
        return executeUpdate(sql,obj);
    }

    @Override
    public List<Vivoly> getAll(int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivoly> list = new ArrayList();
        String sql = "select * from Vivoly limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, from);
            ment.setObject(2, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivoly vivoly = new Vivoly();
                    vivoly.setId(resultSet.getInt(1));
                    vivoly.setName(resultSet.getString(2));
                    vivoly.setPhone(resultSet.getString(3));
                    vivoly.setNeirong(resultSet.getString(4));
                    vivoly.setZhuangtai(resultSet.getInt(5));
                  vivoly.setHuifu(resultSet.getString(6));
                    list.add(vivoly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int getCount() {
        conn = getConnerction();
        String sql = "select count(1) from Vivoly";
        try {
            ment = conn.prepareStatement(sql);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int del(String id) {
        String sql = "DELETE from Vivoly where id in (?)";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }

    @Override
    public int edits(Vivoly Vivoly) {
        String sql = "UPDATE Vivoly SET zhuangtai=?,huifu=? WHERE id=?";
        Object[] obj = {Vivoly.getZhuangtai(),Vivoly.getHuifu(),Vivoly.getId()};
        return executeUpdate(sql, obj);
    }

    @Override
    public List<Vivoly> dan(String name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivoly> list = new ArrayList();
        String sql = "select * from Vivoly where concat(name,phone,neirong,huifu) like ? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, "%" + name + "%");
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivoly vivoly = new Vivoly();
                    vivoly.setId(resultSet.getInt(1));
                    vivoly.setName(resultSet.getString(2));
                    vivoly.setPhone(resultSet.getString(3));
                    vivoly.setNeirong(resultSet.getString(4));
                    vivoly.setZhuangtai(resultSet.getInt(5));
                    vivoly.setHuifu(resultSet.getString(6));
                    list.add(vivoly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int mhdan(String name) {
        conn = getConnerction();
        String sql = "select count(1) from Vivoly where concat(name,phone,neirong,huifu) like ?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, "%" + name + "%");
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Vivoly> ztcx(int name, int from, int pageSixe) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivoly> list = new ArrayList();
        String sql = "select * from Vivoly where zhuangtai like ? limit ?,?";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            ment.setObject(1, "%" + name + "%");
            ment.setObject(2, from);
            ment.setObject(3, pageSixe);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivoly vivoly = new Vivoly();
                    vivoly.setId(resultSet.getInt(1));
                    vivoly.setName(resultSet.getString(2));
                    vivoly.setPhone(resultSet.getString(3));
                    vivoly.setNeirong(resultSet.getString(4));
                    vivoly.setZhuangtai(resultSet.getInt(5));
                    vivoly.setHuifu(resultSet.getString(6));
                    list.add(vivoly);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int mhztcx(int name) {
        conn = getConnerction();
        String sql = "select count(1) from Vivoly where zhuangtai like ?";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, "%" + name + "%");
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

}
